package school.baby.nem165.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.util.Log;

import com.google.tagmanager.DataLayer;
import com.google.tagmanager.TagManager;

public class Utility {


	/**
	 * Encodes MD5 uses's password.
	 * 
	 * @param aPass
	 *            Original password.
	 * @return
	 */
	public static String getMD5Digest(String aPass) {
		MessageDigest digester = null;
		try {
			digester = MessageDigest.getInstance("MD5");
			byte[] bytes = aPass.getBytes();
			digester.update(bytes);

		} catch (NoSuchAlgorithmException e) {
			Log.e("AI", e.getMessage());
		}
		if (digester != null) {
			byte[] lDigestBuff = digester.digest();

			StringBuilder lMutStr = new StringBuilder();
			for (byte b : lDigestBuff) {
				lMutStr.append(String.format("%02x", b));
			}
			return lMutStr.toString();
		}
		return "";
	}

	/**
	   * Push an "openScreen" event with the given screen name. Tags that match that event will fire.
	   */
	  public static void pushOpenScreenEvent(Context context, String screenName) {
	    DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
	    dataLayer.push(DataLayer.mapOf("screenName", screenName, "event", "openScreen"));
	  }

	  /**
	   * Push a "closeScreen" event with the given screen name. Tags that match that event will fire.
	   */
	  public static void pushCloseScreenEvent(Context context, String screenName) {
	    DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
	    dataLayer.push(DataLayer.mapOf("screenName", screenName, "event", "closeScreen"));
	  }
}
